using System;
using System.Windows.Forms;
using FileSystemView.SelectedFsElements;
using HgAdapter;
using HogoLogic.HgFileSystem;

namespace FileSystemView
{
	public partial class HogoFileSystemView : UserControl
	{
		public event EventHandler OnSelectedFsElementChanged;
		
		public HogoFileSystemView()
		{
			InitializeComponent();
		}
		
		public void Open(string projectPath)
		{
			if (projectPath.Length == 0)
				return;
			ProjectCommitInfo.Instance.Init(projectPath);
			directoryView.Open(new HgDirectoryInfo(projectPath));
			hogoFileView.Open(new HgDirectoryInfo(projectPath));
		}

		public GetFilesOptions GetFilesPreferences
		{
			get
			{
				return hogoFileView.Options;
			}
			set
			{
				hogoFileView.Options = value;
			}
		}
		
		public SelectedFsElement SelectedFsElement
		{
			get
			{
				if (directoryView.SelectedFsElement is SelectedDirectory)
					return directoryView.SelectedFsElement;
				return hogoFileView.SelectedFsElement;
			}
		}

		private void directoryView_OnSelectedDirectoryChanged(object sender, System.EventArgs e)
		{
			hogoFileView.Open(directoryView.SelectedDirectory);
			OnSelectedFsElementChanged(sender, e);
		}

		private void hogoFileView_OnSelectedFileChanged(object sender, System.EventArgs e)
		{
			directoryView.UnselectFolder();
			OnSelectedFsElementChanged(sender, e);
		}

		public ContextMenuStrip InnerContextMenuStrip
		{
			get
			{
				if (hogoFileView.ContextMenuStrip == directoryView.ContextMenuStrip)
					return hogoFileView.ContextMenuStrip;
				else
					return null;
			}
			set
			{
				hogoFileView.ContextMenuStrip = value;
				directoryView.ContextMenuStrip = value;
			}
		}
	}
}
